package com.datastructure.test.yanghuitriangle;

public class YangHuiTriangle {

    public static void main(String[] args) {
        YangHuiTriangle yangHuiTriangle = new YangHuiTriangle();
        yangHuiTriangle.generate(5);
    }

    public int[][] generate (int num) {
        // write code here
        int[][] result=new int[num][];
        for (int i = 0; i < num; i++) {
            result[i]=new int[i+1];
        }
        for (int i = 0; i < result.length; i++) {
            for (int j = 0; j < result[i].length; j++) {
                if(j==0||j==(result[i].length-1)){
                    result[i][j]=1;
                } else {
                    if(i>1){
                        result[i][j]=result[i-1][j]+result[i-1][j-1];
                    }
                }
            }
        }
        return result;
    }

    public int[] getRow (int num) {
        // write code here
        int[][] result=new int[num+1][];
        for (int i = 0; i < num+1; i++) {
            result[i]=new int[i+1];
        }
        for (int i = 0; i < result.length; i++) {
            for (int j = 0; j < result[i].length; j++) {
                if(j==0||j==(result[i].length-1)){
                    result[i][j]=1;
                } else {
                    if(i>1){
                        result[i][j]=result[i-1][j]+result[i-1][j-1];
                    }
                }
            }
        }
        return result[num];
    }
}
